package com.yyks.es.dubbo.service.hotel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.yeyks.common.constants.hotel.HotelSourceType;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.hotel.dal.dao.HotelInfoMapper;
import com.yeyks.hotel.dal.dao.RoomCalendarInfoMapper;
import com.yeyks.hotel.dal.dao.RoomInfoMapper;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.dal.domain.RoomCalendarInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yyks.es.dubbo.constants.EsIndexConstant;
import com.yyks.es.dubbo.pojo.model.EsHotelInfo;
import com.yyks.es.dubbo.pojo.model.EsRoomCalendarInfo;
import com.yyks.es.dubbo.pojo.model.EsRoomInfo;
import com.yyks.es.dubbo.pojo.model.base.EsDocType;
import com.yyks.es.dubbo.pojo.model.base.EsGeoPoint;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: dagny
 * @date: 2019/9/29 0029 11:08
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class EsHotelInformationServiceImpl implements EsHotelInformationService {

    private final HotelInfoMapper hotelInfoMapper;

    private final RoomInfoMapper roomInfoMapper;

    private final RoomCalendarInfoMapper roomCalendarInfoMapper;

    /**
     * 1.查询酒店房间房态的各种信息
     * 2.查询数据用于存入Es中
     *
     * @param hotelId 酒店Id
     * @return 返回结果
     */
    @Override
    public EsHotelInfo searchHotel(Integer hotelId) {

        EsHotelInfo esHotelInfo = null;
        HotelInfo hotelInfo = hotelInfoMapper.selectOne(new QueryWrapper<HotelInfo>()
                .eq(HotelInfo.ID, hotelId));
        if (null != hotelInfo) {
            esHotelInfo = MyBeanUtils.convert(hotelInfo, EsHotelInfo.class);
            esHotelInfo.setHotelId(EsIndexConstant.ID_PREX_YYKS_HOTEL + hotelId);
            esHotelInfo.setSourceType(HotelSourceType.YYKS);
            esHotelInfo.setSourceId(String.valueOf(hotelId));
            EsDocType esDocType = new EsDocType();
            esDocType.setName(EsIndexConstant.JOIN_TYPE_HOTEL);
            esHotelInfo.setDocType(esDocType);
            EsGeoPoint point = new EsGeoPoint();
            if (null != hotelInfo.getPositionY() && null != hotelInfo.getPositionX()) {
                point.setLat(hotelInfo.getPositionY());
                point.setLon(hotelInfo.getPositionX());
            }
            esHotelInfo.setLocation(point);
        }
        return esHotelInfo;
    }

    /**
     * 1.批量查询酒店信息
     * 2.查询数据用于存入Es中
     *
     * @param hotelIdList 酒店Id集合
     * @return 返回结果
     */
    @Override
    public List<EsHotelInfo> searchHotelByHotelIdList(List<Integer> hotelIdList) {

        List<EsHotelInfo> esHotelInfoList = Lists.newArrayList();
        EsHotelInfo esHotelInfo;
        List<HotelInfo> hotelInfoList = hotelInfoMapper.selectList(new QueryWrapper<HotelInfo>()
                .in(HotelInfo.ID, hotelIdList));
        if (!CollectionUtils.isEmpty(hotelInfoList)) {
            for (HotelInfo hotelInfo : hotelInfoList) {
                EsDocType esDocType = new EsDocType();
                EsGeoPoint point = new EsGeoPoint();
                esHotelInfo = MyBeanUtils.convert(hotelInfo, EsHotelInfo.class);
                esHotelInfo.setHotelId(EsIndexConstant.ID_PREX_YYKS_HOTEL + hotelInfo.getId());
                esHotelInfo.setSourceType(HotelSourceType.YYKS);
                esHotelInfo.setSourceId(String.valueOf(hotelInfo.getId()));
                esDocType.setName(EsIndexConstant.JOIN_TYPE_HOTEL);
                esHotelInfo.setDocType(esDocType);
                if (null != hotelInfo.getPositionY() && null != hotelInfo.getPositionX()) {
                    point.setLat(hotelInfo.getPositionY());
                    point.setLon(hotelInfo.getPositionX());
                }
                esHotelInfo.setLocation(point);
                esHotelInfoList.add(esHotelInfo);
            }
        }
        return esHotelInfoList;
    }


    /**
     * 根据房间Id集合 得到房间信息集合
     *
     * @param roomIdList 房间Id集合
     * @return 返回结果
     */
    @Override
    public List<EsRoomInfo> searchRoomByIds(List<Integer> roomIdList) {

        List<EsRoomInfo> esRoomInfoList = null;
        List<RoomInfo> roomInfoList = roomInfoMapper.selectList(new QueryWrapper<RoomInfo>()
                .in(RoomInfo.ID, roomIdList));
        if (!CollectionUtils.isEmpty(roomInfoList)) {
            esRoomInfoList = convertEsRoomInfoList(roomInfoList);
        }
        return esRoomInfoList;
    }


    /**
     * 根据酒店Id 得到房间集合信息
     *
     * @param hotelId 酒店ID
     * @return 返回集合
     */
    @Override
    public List<EsRoomInfo> searchRoomList(Integer hotelId) {

        List<EsRoomInfo> esRoomInfoList = null;
        List<RoomInfo> roomInfoList = roomInfoMapper.selectList(new QueryWrapper<RoomInfo>()
                .eq(RoomInfo.HOTEL_ID, hotelId));
        if (!CollectionUtils.isEmpty(roomInfoList)) {
            esRoomInfoList = convertEsRoomInfoList(roomInfoList);
        }
        return esRoomInfoList;
    }

    /**
     * 根据酒店ID集合 得到房间集合信息
     *
     * @param hotelIdList 酒店ID集合信息
     * @return 返回集合
     */
    @Override
    public List<EsRoomInfo> searchRoomListByHotelList(List<Integer> hotelIdList) {

        List<EsRoomInfo> esRoomInfoList = null;
        List<RoomInfo> roomInfoList = roomInfoMapper.selectList(new QueryWrapper<RoomInfo>()
                .in(RoomInfo.HOTEL_ID, hotelIdList));
        if (!CollectionUtils.isEmpty(roomInfoList)) {
            esRoomInfoList = convertEsRoomInfoList(roomInfoList);
        }
        return esRoomInfoList;
    }

    @Override
    public List<EsRoomCalendarInfo> searchRoomCalendarByIds(List<Integer> calendarIdList) {

        List<EsRoomCalendarInfo> esRoomCalendarInfoList = null;
        List<RoomCalendarInfo> roomCalendarInfoList = roomCalendarInfoMapper.selectList(new QueryWrapper<RoomCalendarInfo>()
                .in(RoomCalendarInfo.ID, calendarIdList));
        if (!CollectionUtils.isEmpty(roomCalendarInfoList)) {
            esRoomCalendarInfoList = convertEsRoomCalendarInfoList(roomCalendarInfoList);
        }
        return esRoomCalendarInfoList;
    }

    /**
     * 根据房间集合得到房态集合
     *
     * @param esRoomInfoList 房间集合
     * @return 返回房态集合
     */
    @Override
    public List<EsRoomCalendarInfo> searchRoomCalendarList(List<EsRoomInfo> esRoomInfoList) {

        List<EsRoomCalendarInfo> roomCalendarInfoList = Lists.newArrayList();

        if (!CollectionUtils.isEmpty(esRoomInfoList)) {
            for (EsRoomInfo roomInfo : esRoomInfoList) {
                List<RoomCalendarInfo> roomCalendarInfos = roomCalendarInfoMapper.selectFutureOneMonthDataByRoomId(roomInfo.getId());
                if (!CollectionUtils.isEmpty(roomCalendarInfos)) {
                    roomCalendarInfoList.addAll(convertEsRoomCalendarInfoList(roomCalendarInfos));
                }
            }
        }
        return roomCalendarInfoList;
    }

    /**
     * ES 的房间 对象的转换 （PO --> ES_PO）
     *
     * @param roomInfoList 房间集合
     * @return 返回Es 房间集合
     */
    private List<EsRoomInfo> convertEsRoomInfoList(List<RoomInfo> roomInfoList) {
        List<EsRoomInfo> esRoomInfoList = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(roomInfoList)) {
            esRoomInfoList = MyBeanUtils.convertArrayList(roomInfoList, EsRoomInfo.class);
            for (EsRoomInfo roomInfo : esRoomInfoList) {
                roomInfo.setRoomId(EsIndexConstant.ID_PREX_YYKS_ROOM + roomInfo.getId());
                roomInfo.setSourceType(HotelSourceType.YYKS);
                EsDocType docType = new EsDocType();
                docType.setName(EsIndexConstant.JOIN_TYPE_ROOM);
                docType.setParent(EsIndexConstant.ID_PREX_YYKS_HOTEL + roomInfo.getHotelId());
                roomInfo.setDocType(docType);
                roomInfo.setSourceId(String.valueOf(roomInfo.getId()));
            }
        }
        return esRoomInfoList;
    }

    /**
     * ES 的房态 对象的转换 （PO --> ES_PO）
     *
     * @param roomCalendarInfoList 房态集合
     * @return 返回Es 房态集合
     */
    private List<EsRoomCalendarInfo> convertEsRoomCalendarInfoList(List<RoomCalendarInfo> roomCalendarInfoList) {

        List<EsRoomCalendarInfo> esRoomCalendarInfoList = MyBeanUtils.convertArrayList(roomCalendarInfoList,
                EsRoomCalendarInfo.class);
        for (EsRoomCalendarInfo calendarInfo : esRoomCalendarInfoList) {
            calendarInfo.setCalendarId(EsIndexConstant.ID_PREX_YYKS_CALENDAR + calendarInfo.getId());
            EsDocType docType = new EsDocType();
            docType.setName(EsIndexConstant.JOIN_TYPE_CALENDAR);
            docType.setParent(EsIndexConstant.ID_PREX_YYKS_ROOM + calendarInfo.getRoomId());
            calendarInfo.setDocType(docType);
            calendarInfo.setSourceType(HotelSourceType.YYKS);
            calendarInfo.setSourceId(String.valueOf(calendarInfo.getId()));
        }
        return esRoomCalendarInfoList;
    }
}
